package com.caimiao.miao.library.usb.host;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;

import com.caimiao.miao.library.utils.tip.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class UsbHostInterface {
    public static final int REQUEST_USB_PERMISSION = 1001;
    private static final String ACTION_USB_PERMISSION = "com.caimiao.miao.USB_PERMISSION";
    private UsbHostReceiver mUsbReceiver;
    private UsbManager mUsbManager;
    private PendingIntent mPermissionIntent;

    private class UsbHostReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (ACTION_USB_PERMISSION.equals(action)) {
                synchronized (this) {
                    UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                    if (device != null) {
                        if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                            ToastUtil.show("获取USB权限成功");
                        } else {
                            ToastUtil.show("获取USB权限失败");
                        }
                    }
                }
            } else if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
                ToastUtil.show("插入USB设备");
            } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
                ToastUtil.show("断开USB设备");
            }
        }
    }

    public UsbHostInterface(Context context) {
        mUsbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
        IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
        filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
        filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
        mUsbReceiver = new UsbHostReceiver();
        context.registerReceiver(mUsbReceiver, filter);
        mPermissionIntent = PendingIntent.getBroadcast(context, REQUEST_USB_PERMISSION, new Intent(ACTION_USB_PERMISSION), 0);
    }

    public void onDestroy(Context context) {
        context.unregisterReceiver(mUsbReceiver);
    }

    public List<UsbDevice> getDevices() {
        HashMap<String, UsbDevice> deviceMap = mUsbManager.getDeviceList();
        ToastUtil.show("USB设备个数：" + deviceMap.size());
        List<UsbDevice> deviceList = new ArrayList<>();
        for (final UsbDevice device : deviceMap.values()) {
            deviceList.add(device);
        }
        return deviceList;
    }

    public UsbDevice getDevice(int vendorId, int productId) {
        HashMap<String, UsbDevice> deviceMap = mUsbManager.getDeviceList();
        for (final UsbDevice device : deviceMap.values()) {
            if (vendorId == device.getVendorId() && productId == device.getProductId()) {
                return device;
            }
        }
        ToastUtil.show("未找到对应USB设备");
        return null;
    }

    public boolean hasPermission(UsbDevice device) {
        return mUsbManager.hasPermission(device);
    }

    public void requestUsbPermission(UsbDevice device) {
        if (!hasPermission(device)) {
            mUsbManager.requestPermission(device, mPermissionIntent);
        }
    }
}
